import React from 'react'
import { Redirect, Route } from 'react-router'
import { FC } from 'react-router/node_modules/@types/react'
import { UserData } from '../../consts/auth'
import { isAuth } from '../../helpers/auth'
interface PrivateAdminRouteProps {
    path:string,
    component: React.ComponentType<any>
}
const PrivateAdminRoute: FC<PrivateAdminRouteProps> = ({
    component: Component,
    ...rest
}: PrivateAdminRouteProps) => {
    return (
        <Route {...rest} render={
            (props)=>{
                const userData = isAuth();
                if( userData){
                    const {
                        user
                    } = userData as UserData;
                    if( user.role === 1 ){
                        return <Component {...props} />
                    } else {
                        return <Redirect to="/user/dashboard" />

                    }
                } else {
                    return <Redirect to="/signin" />
                }
            }
        }>
            
        </Route>
    )
}

export default PrivateAdminRoute
